<?php

// 本类由系统自动生成

namespace Home\Controller ;

use Think\Controller ;

class IndexController extends Controller
{

    protected $UserLoginModel = null ;
    protected $UserBaseModel  = null ;

    public function __construct ()
    {
        parent::__construct () ;
        $this->UserLoginModel = new \Home\Model\UserLoginModel() ;
        $this->UserBaseModel  = new \Home\Model\UserBaseModel() ;
    }

    public function index ()
    {


        $rs = $this->UserBaseModel->getUserBaseInfor () ;
        foreach ( $rs as
                $key =>
                $value )
        {
            $this->assign ( $key , $value ) ;
            echo $key . "<br>" ;
            dump ( $value ) . "<br>" ;
        }
    }

    //添加新用户操作
    function addNewUser ()
    {

        $UID              = $_POST[ 'UID' ] ;
        $UName            = $_POST[ 'userName' ] ;
        $UTelephoneNumber = $_POST[ 'userTelephoneNumber' ] ;
        // $UTelephoneNumber = "23424" ;
        $UQQ              = $_POST[ 'userQQ' ] ;
        $UEmail           = $_POST[ 'userEmail' ] ;
        $UDepartment      = $_POST[ 'userDepartment' ] ;
        $Password         = '1234567' ;
        $PictureName      = $this->uploadImage () ;



        $this->UserBaseModel->startTrans () ;
        $UserBase = $this->UserBaseModel->addUserBaseInfor ( $UID , $UName ,
                $UTelephoneNumber , $UQQ , $UEmail , $UDepartment , $PictureName ) ;
        $Count    = $this->UserLoginModel->addUserCount ( $UID , $Password ) ;

        if ( $UserBase && $Count )
        {
            $this->UserBaseModel->commit () ;
            $this->success ( '添加用户成功！' , U ( 'admincontact' ) ) ;
        }
        else
        {
            $this->UserBaseModel->rollback () ;
            $this->success ( '添加用户失败！' ) ;
        }
    }

    // 修改用户基本信息操作
    function updateUserBaseInforByUID ()
    {
        $UID              = I ( 'UID,' , null ) ;
        $UName            = I ( 'userName' , null ) ;
        $UTelephoneNumber = I ( 'userTelephoneNumber' , null ) ;
        $UQQ              = I ( 'userQQ' , null ) ;
        $UEmail           = I ( 'userEmail' , null ) ;
        $UDepartment      = I ( 'userDepartment' , null ) ;
        $rs               = $this->UserBaseModel->updateUserBaseInforByUID ( $UID ,
                $UName , $UTelephoneNumber , $UQQ , $UEmail , $UDepartment ) ;
        if ( $rs )
        {
            $this->success ( '修改用户基本信息成功！' ) ;
        }
        else
        {
            $this->error ( '修改用户基本信息失败！' ) ;
        }
    }

    //用户登录操作
    function checkUPassword ()
    {
        $UID      = I ( 'UID' , null ) ;
        $Password = I ( 'userPassword' , null ) ;
        $Status   = $this->UserLoginModel->getUserStatusByUID ( $UID ) ;
        if ( $this->UserLoginModel->checkUPassword ( $UID , $Password ) )
        {
            session ( 'UID' , $UID ) ;
            switch ( $Status )
            {
                case 0:
                    $this->redirect ( 'contact' ) ;
                    break ;
                case 1:
                    $this->redirect ( 'admincontact' ) ;
                    break ;
                default :
                    break ;
            }
        }
        else
        {
            $this->error ( '密码错误！' ) ;
        }
    }

    //用户修改密码操作
    function changeUPassword ()
    {
        $UID          = session ( 'UID' ) ;
        $userPassword = I ( 'userPassword' , null ) ;
        $newPassword  = I ( 'newPassword' , null ) ;
        $rePassword   = I ( 'rePassword' , null ) ;
        //验证密码是否正确
        if ( $this->UserLoginModel->checkUPassword ( $UID , $userPassword ) )
        {
            if ( $newPassword === $rePassword && $newPassword != null )
            {
                if ( $this->UserLoginModel->updateUPassword ( $UID ,
                                $newPassword ) )
                {
                    $this->success ( '修改密码成功！' ) ;
                }
                else
                {
                    $this->error ( '密码修改失败！' ) ;
                }
            }
            $this->error ( '密码输入不一致，请重新输入！' ) ;
        }
        $this->error ( '密码错误！' ) ;
    }

    //用户退出登录操作
    function loginOut ()
    {
        session ( 'UID' , null ) ;
        $this->success ( '退出登录成功！' ) ;
    }

    //管理员删除用户操作
    function deleteUser ()
    {
        $UQQ = $_GET[ 'UID' ] ;

        $userID = $UQQ ; //被删除用户的ID
        $rs     = $this->UserBaseModel->deleteUser ( $userID ) ;
        if ( $rs )
        {
            $this->success ( '删除用户成功！' ) ;
        }
        else
        {
            $this->error ( '删除用户失败！' ) ;
        }
    }

    //用户登录页面
    function login ()
    {
        $this->display () ;
    }

    function contact ()
    {
        $UID = session ( 'UID' ) ;
        if ( session ( 'UID' ) )
        {
            $UserName = $this->UserBaseModel->getUserNameByUID ( $UID ) ;
            $this->assign ( 'userName' , $UserName ) ;

            //用户图像等
            $rs = $this->UserBaseModel->getUserBaseInfor () ;
            // dump ( $rs ) ;

            foreach ( $rs as
                    $key =>
                    $value )
            {
                $this->assign ( $key , $value ) ;
            }


            //$this->assign ( $rs ) ;
            $this->display () ;
        }
        else
        {
            $this->error ( '登录超时！' ) ;
        }
    }

    //管理员页面
    function admincontact ()
    {

        $UID    = session ( 'UID' ) ;
        //获取用户身份
        $Status = $this->UserLoginModel->getUserStatusByUID ( $UID ) ;
        if ( $UID && $Status )
        {
            $UserName = $this->UserBaseModel->getUserNameByUID ( $UID ) ;
            $this->assign ( 'userName' , $UserName ) ;
            // $this->assign ( 'UID' , $UID ) ;
            //用户图像等
            $rs       = $this->UserBaseModel->getUserBaseInfor () ;
            // dump ( $rs ) ;

            foreach ( $rs as
                    $key =>
                    $value )
            {
                $this->assign ( $key , $value ) ;
            }



            $this->display () ;
        }
        else
        {
            $this->error ( '登录超时！' ) ;
        }
    }

    //上传图片
    function uploadImage ()
    {

        if ( $_FILES[ "file" ][ "error" ] > 0 )
        {
            echo "Error: " . $_FILES[ "file" ][ "error" ] . "<br />" ;
            return false ;
        }
        else
        {
            //echo "Upload: " . $_FILES[ "file" ][ "name" ] . "<br />" ;
            //echo "Type: " . $_FILES[ "file" ][ "type" ] . "<br />" ;
            //echo "Size: " . ($_FILES[ "file" ][ "size" ] / 1024) . " Kb<br />" ;
            // echo "Stored in: " . $_FILES[ "file" ][ "tmp_name" ] ;
            if ( move_uploaded_file ( $_FILES[ "file" ][ "tmp_name" ] ,
                            dirname ( __FILE__ ) . '/IMG/' . $_FILES[ "file" ][ "name" ] ) )
            {

                //从临时地址，移动到指定地址
                //echo '<br/>成功上传文件<br>' ;
                //获取图片路径
                $path     = dirname ( __FILE__ ) . '/IMG/' . $_FILES[ "file" ][ "name" ] ;
                $filename = $_FILES[ "file" ][ "name" ] ;
                return $filename ;
            }
            return false ;
        }
        return false ;
    }

}
